package com.liuyc.lib.image.loader

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.widget.ImageView
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory
import com.bumptech.glide.Glide
import com.bumptech.glide.Priority
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.Request
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.BitmapImageViewTarget
import com.bumptech.glide.request.target.Target

/**
 * ImageLoaderManager
 * 本应用 使用的 图片加载器
 * 刘隽
 */
class ImageLoaderManager {

    //静态类来存放 ImageLoaderManager 的单例
    private object SingletonHolder {
        val holder = ImageLoaderManager()
    }

    companion object {
        fun getInstance(): ImageLoaderManager {
            return SingletonHolder.holder
        }
    }

    /**
     * 最基础的加载方案，会缓存
     */
    fun loaderImage(imageView: ImageView, url: String?) {
        Glide.with(imageView.context).load(url).apply(initCommonRequestOption()).into(imageView)
    }

    /**
     * 最基础的加载方案，会缓存
     */
    fun loaderImage(imageView: ImageView, url: String?, width: Int = 0, height: Int = 0) {
        Glide.with(imageView.context).load(url).override(width, height).apply(initCommonRequestOption()).into(imageView)
    }

    /**
     * 暂停图片加载
     */
    fun pauseLoadImage(context: Context) {
        Glide.with(context).pauseRequests()
    }

    /**
     * 继续加载图片
     */
    fun resumeLoadImage(context: Context){
        Glide.with(context).resumeRequests()
    }

    /**
     * 加载圆形图片方法
     *
     * @param imageView
     * @param url
     */
    fun displayImageForCircle(
            imageView: ImageView,
            url: String?
    ) {
        Glide.with(imageView.context)
                .asBitmap()
                .load(url)
                .apply(initCommonRequestOption())
                .into(object : BitmapImageViewTarget(imageView) {
                    override fun setResource(resource: Bitmap?) {
                        val circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(
                                        imageView.resources,
                                        resource
                                )
                        circularBitmapDrawable.isCircular = true
                        imageView.setImageDrawable(circularBitmapDrawable)
                    }
                })
    }


    @SuppressLint("CheckResult")
    private fun initCommonRequestOption(): RequestOptions {
        val options = RequestOptions()
        options.placeholder(R.drawable.place_holder)
                .error(R.drawable.place_holder)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .skipMemoryCache(false)
                .priority(Priority.NORMAL)
        return options
    }

}